package com.ruoyi.tcp.entity.read;

import com.ruoyi.tcp.entity.ClientCustomerBaseEntity;
import com.ruoyi.tcp.util.CrcUtil;
import com.ruoyi.tcp.util.TcpAesUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @author 蒋翔 SY100242
 * @date 2024/8/7 17:18
 * @email SY100242@hnlens.com
 */
@Setter
@Getter
@ToString
public class ReadCustomerMessageEntity extends ClientCustomerBaseEntity {

    //数据发生时间
    private LocalDateTime occurTime;
    //设备编号
    private Long deviceNumber;
    //设备序列号(RFID射频号)
    private String deviceSerializeNumber;
    //经度
    private Integer longitude;
    //纬度
    private Integer latitude;

    @Override
    public byte[] getOtherBytesClient() {
        ByteBuf buffer = Unpooled.buffer();
        if(Objects.nonNull(occurTime)) {
            long epochSecondSecond = occurTime.toEpochSecond(ZoneOffset.of("+8"));
            byte[] epochTimeBytes = TcpAesUtil.longToByteArray8(epochSecondSecond);
            buffer.writeBytes(epochTimeBytes);
        }
        if(Objects.nonNull(deviceNumber)) {
            byte[] deviceNumberBytes = TcpAesUtil.longToByteArray16(deviceNumber);
            buffer.writeBytes(deviceNumberBytes);
        }
        if(StringUtils.hasText(deviceSerializeNumber)) {
            byte[] deviceSerializeNumberBytes = TcpAesUtil.hex2byte(deviceSerializeNumber);
            buffer.writeBytes(deviceSerializeNumberBytes);
        }
        if(Objects.nonNull(longitude)) {
            byte[] bytes = TcpAesUtil.intToByteArray(longitude);
            buffer.writeBytes(bytes);
        }
        if(Objects.nonNull(latitude)) {
            byte[] bytes = TcpAesUtil.intToByteArray(latitude);
            buffer.writeBytes(bytes);
        }
        return ByteBufUtil.getBytes(buffer);
    }


    public static void main(String[] args) {
        ReadCustomerMessageEntity entity = new ReadCustomerMessageEntity();
        entity.setVersion(2);
        entity.setSerializeNumber(24L);
        entity.setTypeStr("0x2");
        LocalDateTime parse = LocalDateTime.parse("2021-03-10 17:29:02", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        entity.setSendTime(parse);
        LocalDateTime parse1 = LocalDateTime.parse("2021-03-10 16:51:57", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        entity.setOccurTime(parse1);
        entity.setDeviceNumber(12345L);
        entity.setDeviceSerializeNumber("AE23C4D23E08CA7D34D53B1E");
        byte[] encryptData = entity.createEncryptBytes("Z4T6U7RL08VJ5Y36");



        String channelNoStr = TcpAesUtil.getChannelNoStr(101L);
        byte[] channelBytes = channelNoStr.getBytes(StandardCharsets.UTF_8);
        byte[] finalSendData = TcpAesUtil.createFinalSendData(encryptData, channelBytes);
        String s = new String(finalSendData, StandardCharsets.UTF_8);
        System.out.println(s);
    }
}
